package com.zhongxin.wymovieticketfragment;

import java.security.PublicKey;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.Network;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.zhongxin.wymovieapi.Client;
import com.zhongxin.wymovieticket.HotMovieDetailActivity;
import com.zhongxin.wymovieticket.R;
import com.zhongxin.wymovieticket.adapter.HotMovieAdapter;
import com.zhongxin.wymovieticket.getdata.DataStore;
import com.zhongxin.wymovieticketentity.HotContent;
import com.zhongxin.wymovieticketentity.HotMovies;
import com.zhoujing.utils.JsonUtils;
import com.zhoujing.utils.JsonUtils.JsonObjectCallBack;
import com.zhoujing.utils.JsonUtils.JsonStringCallBack;

import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class HotMovieFragment extends Fragment implements OnItemClickListener {

	private List<HotMovies> list;
	private PullToRefreshListView mPullRefreshListView;
	private HotMovieAdapter hotMovieAdapter;
	private View enptyview;
	private RequestQueue queue;
	private StringRequest stringRequest;
	private String cityurl = "http://piao.163.com/m/base_data.html?updateTime=&apiVer=14&mobileType=android&deviceId=08:00:27:f4:e2:00&channel=netease&ver=4.8&city=440300";	
	private String url2 = "http://flv.bn.netease.com/videolib3/1408/26/isfVg8137/HD/isfVg8137-mobile.mp4";
	private String url;
	public HotMovieFragment() {
		// TODO Auto-generated constructor stub
		
	}
	public void init(String url){
		this.url=url;
	} 

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		queue = Volley.newRequestQueue(getActivity());
		View view = inflater.inflate(R.layout.fragment_hotmovie, container,
				false);
		

		list = new ArrayList<HotMovies>();

		mPullRefreshListView = (PullToRefreshListView) view
				.findViewById(R.id.listview);
		mPullRefreshListView.setMode(Mode.BOTH);

		enptyview = LayoutInflater.from(getActivity()).inflate(
				R.layout.enpty_view, null, false);
		mPullRefreshListView.setEmptyView(enptyview);
		mPullRefreshListView
				.setOnRefreshListener(new OnRefreshListener<ListView>() {

					@Override
					public void onRefresh(
							PullToRefreshBase<ListView> refreshView) {
						// TODO Auto-generated method stub

						String label = DateUtils.formatDateTime(getActivity()
								.getApplicationContext(), System
								.currentTimeMillis(),
								DateUtils.FORMAT_SHOW_TIME
										| DateUtils.FORMAT_SHOW_DATE
										| DateUtils.FORMAT_ABBREV_ALL);

						// Update the LastUpdatedLabel
						refreshView.getLoadingLayoutProxy()
								.setLastUpdatedLabel(label);

						// refreshView.onRefreshComplete();
						// hotMovieAdapter.notifyDataSetChanged();
						// Do work to refresh the list here
						// new GetDataTask().execute();
					}

				});

		getData();// 初始化数据

		mPullRefreshListView.setOnItemClickListener(this);
		//getCityData();

		return view;
	}
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//getCityData();
	}

	public void getData() {
		JsonUtils.parseJsonObject(getActivity(), url, new JsonObjectCallBack() {

			@Override
			public void jsonObjectCallBack(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				Log.i("---------------------",
						"---------------" + jsonObject.toString());
				HotContent hotContent = new HotContent();
				try {
					int rtecode = jsonObject.getInt("retcode");
					Log.i("---------------------", "---------------" + rtecode);
					String retdesc = jsonObject.getString("retdesc");
					JSONArray movieListArray = jsonObject.getJSONArray("list");

					for (int i = 0; i < movieListArray.length(); i++) {
						HotMovies hotMovies = new HotMovies();
						hotMovies.parseJson(movieListArray.getJSONObject(i));
						Log.i("---------------------", "---------------"
								+ hotMovies.getDuration());
						list.add(hotMovies);
					}
					// System.out.println("hotmovie==");
					DataStore.getInstance().appendHotMoviesEntities(list);
					hotMovieAdapter = new HotMovieAdapter(list, queue,
							getActivity());
					hotMovieAdapter.notifyDataSetChanged();
					mPullRefreshListView.setAdapter(hotMovieAdapter);

				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

		}, queue);

	}
	


	

	/**
	 * listview的点击事件
	 * 
	 * @param parent
	 * @param view
	 * @param position
	 * @param id
	 */
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		Intent hotMovieDetailintent = new Intent(getActivity(),
				HotMovieDetailActivity.class);
		Bundle bundle = new Bundle();
		bundle.putSerializable("hotmovie", list.get(position));
		hotMovieDetailintent.putExtra("detail", bundle);
		startActivity(hotMovieDetailintent);

	}

}
